<?php
/**
 * Category is an instance of CActiveRecord (sytem.db.ar). 
 *
 * CActiveRecord is the base class for classes representing relational data. 
 *
 * Eduow :  International system for education (http://www.eduow.com)
 * Copyright 2010, Nikolay Stoitsev
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.models
 * @inheritance   class Category >> CActiveRecord >> CModel >> CComponent
 * @implements    ArrayAccess, Traversable, IteratorAggregate
 * @since         Eduow v 0.0.3
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class Category extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'category':
	 * @var integer $id
	 * @var integer $parent
	 * @var string $title
	 * @var string $description
	 * @var string $language
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName(){
		return 'category';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules(){
		return array(
			array('title', 'required'),
			array('parent', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>50),
			array('language', 'length', 'max'=>3),
			array('description', 'safe'),
			array('id, parent, title, description, language', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations(){
		return array(
			'Posts' => array(self::HAS_MANY, 'Post', 'category_id'),
			'getparent' => array(self::BELONGS_TO, 'Category', 'parent'),
			'childs' => array(self::HAS_MANY, 'Category', 'parent'),
		);
		
	}

	/**
	 * Returns the default named scope that should be implicitly applied to all queries for this model.
	 */
	public function defaultScope(){
		return array(
    			'condition'=>'language="'.Yii::app()->language.'"',
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'id' => Yii::t('yii','Id'),
			'parent' => Yii::t('yii','Parent'),
			'title' => Yii::t('yii','Title'),
			'description' => Yii::t('yii','Description'),
			'language' => Yii::t('yii','Language'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search(){

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('parent',$this->parent);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('description',$this->description,true);

		$criteria->compare('language',$this->language,true);

		return new CActiveDataProvider('Category', array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Retrieves a list of element's child records based on parent field in the database
	 * @return array of record's child element 
	 */
	public function getChilds($id) {
		foreach(Category::model()->findAll('parent = ' . $id) as $model) {
			$row['text'] ="<a href=\"/category/view/id/{$model->id}\" ";
			$row['click']="onclick=\"window.location.href='#category/view/id/{$model->id}';get_content('category/view/id/{$model->id}')\"";
			$row['texte'] = ">{$model->title}</a>";
			$row['children'] = Category::getChilds($model->id);
			$data[] = $row;
		}
		return $data;
	}

	/**
	 * Retrieves a list of element's child 
	 * @return array of record's child element 
	 */
	public function getChildsId($id) {
		foreach(Category::model()->findAll('parent = ' . $id) as $model) {
			$data[] = $model->id;
			Category::getChildsId($model->id);
		}
		return $data;
	}

	/**
	 * Buyld full hierarchical tree of all categories
	 * @return array of hierarchical ordered categories
	 */
	public function listing(){
		$models = Category::model()->findAll('parent = 0');
		foreach($models as $model) {
			$row['text'] ="<a href=\"/category/view/id/{$model->id}/{$model->title}\" ";
			$row['click']="onclick=\"window.location.href='#category/view/id/{$model->id}/{$model->title}';get_content('category/view/id/{$model->id}')\"";
			$row['texte'] = ">{$model->title}</a>";
			$row['children'] = Category::getChilds($model->id);
			$data[] = $row;
		}	
		return $data;
	}

	/**
	 * @return array of childs
	 */
	public function getListed() {
		$subitems = array();
		if($this->childs) foreach($this->childs as $child) {
			$subitems[] = $child->getListed();
		}
		$returnarray = array('label' => $this->title, 'url' => array('category/view', 'id' => $this->id));
		if($subitems != array()) $returnarray = array_merge($returnarray, array('items' => $subitems));
		return $returnarray;
	}
}
